import Vue from "vue";
import Router from "vue-router";
import store from "@/store";
import { getToken } from "@/plugins/utils/auth";
import { NO_AUTH } from '@/config/setting'
import { Message } from "element-ui";
import dataConnection from "./dataConnection/index"; // 数据引接路由
import ontologyModel from "./ontologyModel/index"; // 本体建模路由
import knowledgeGenerate from "./knowledgeGenerate/index"; // 知识生成路由
import knowledgeMap from "./knowledgeMap/index"; // 知识管理路由
import knowledgeBase from "./knowledgeBase/index"; // 基准知识库路由
import knowledgeComputation from "./knowledgeComputation/index"; // 知识计算路由
import statisticalAnalysis from "./statisticalAnalysis/index"; // 统计分析路由
// import situationalInfo from "./situationalInfo/index"; // 态势信息路由
import setting from "./setting/index"; // 系统设置路由
import search from "./search/index"; // 智能检索路由


Vue.use(Router);

export const constantRoutes = [
  {
    path: "/login",
    component: () => import("@/views/login/login.vue"),
    hidden: true,
  },
];

export const asyncRoutes = [
  ...dataConnection,
  ...ontologyModel,
  ...knowledgeGenerate,
  ...knowledgeMap,
  // ...knowledgeBase,
  ...knowledgeComputation,
  ...statisticalAnalysis,
  // ...situationalInfo,
  ...setting,
  ...search,
  {
    hidden: true,
    path: "/konwledgeAnalyse",
    component: () => import("@/views/layout/home"),
    meta: {
      title: "图谱分析",
      icon: "icon-shuju",
      roles: ["superAdmin", "commonAdmin"],
    },
    children: [
      {
        path: "index",
        component: () => import("@/views/konwledgeAnalyse/index"),
        name: "konwledgeAnalyse",
        meta: {
          title: "图谱分析",
          roles: ["superAdmin", "commonAdmin"],
        },
      },
    ],
  }
];

// 设置重定向
const firstPath = asyncRoutes[0].path
asyncRoutes.push(
  { path: "*", redirect: NO_AUTH ? firstPath : "/login", hidden: true },
)

const router = new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes,
});
const whiteList = ["/login"];

router.beforeEach(async (to, from, next) => {
  // 如果设置了不登录 则不会获取token
  let hasToken = NO_AUTH || getToken();
  if (hasToken) {
    if (to.path === "/login") {
      next();
    } else {
      const hasRoles =
        store.state.userInfo.roles && store.state.userInfo.roles.length > 0;
      if (hasRoles) {
        next();
      } else {
        try {
          store.commit("userInfo/setRoles");
          const roles = store.state.userInfo.roles;
          const accessRoutes = await store.dispatch(
            "permission/generateRoutes",
            roles
          );
          accessRoutes.forEach((route) => router.addRoute(route));
          next({ ...to, replace: true });
        } catch (error) {
          console.log("error", error);
          await store.commit("userInfo/resetToken");
          Message.error(error || "Has Error");
          next(`/login`);
        }
      }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next();
    } else {
      next(`/login`);
    }
  }
});

const originalPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};
export default router;
